<?php
namespace app\admin\service;
use think\facade\Db;
use app\admin\model\SystemShieldIp as ShieldIpModel;

class ShieldIp extends BaseService
{
	/*
    * 初始
    */
    public function __construct(){
        $this->model = new ShieldIpModel();
    }
    
    /*
    * 添加错误数量
    * $data 
    */
    public function addErrorNum(int $buss_type,string $user_code,string $login_ip){
        $param = [
            "buss_type"=>$buss_type,
            "user_code"=>$user_code,
            "login_ip"=>$login_ip
        ];
        if( $shield = $this->model->where($param) ->find() ){
            $shield->error_num = $shield->error_num + 1;
            $shield->save();
            return $shield->error_num;
        }else{
            $param['error_num'] = 1;
           $this->model->create($param);
            return 1;
        }
    }
    
    
    /*
    * 添加错误数量
    * $data 
    */
    public function verifyIp(int $buss_type,string $user_code,string $login_ip){
        $param = [
            "buss_type"=>$buss_type,
            "user_code"=>$user_code,
            "login_ip"=>$login_ip
        ];
        $res = true;
        $loginErrorNum = getSystem('base_login_error_num') ?: 0;
        if($loginErrorNum > 0){
            $shield = $this->model->where($param) ->find();
            if( $shield && $shield->error_num >= $loginErrorNum){
                $res = false;
            }
        }
        return $res;
    }
    
}